<?php
/**
 * Created by PhpStorm.
 * User: jim
 * Date: 2017-01-10
 * Time: 20:05
 */

namespace App\Http\Middleware;


use App\Models\Count;
use \App\Models\IsMobile;
use Closure;


/**
 *访客统计中间件
 */

class IndexCount
{
    public function handle($request, Closure $next)
    {
        $timeip=date("Ymd",time()).$_SERVER['REMOTE_ADDR'];
        $res=Count::where('ip',$timeip)->first();
        $is_m=new IsMobile();

        //判断访客在24小时内是否有记录，有记录则取出，否则创建
        if($res==""){
            $ob=new Count();
            $ob->ip=$timeip;
        }else{
            $ob=$res;
        }
        $user_agent = $_SERVER['HTTP_USER_AGENT'];

        //判断访客最后一次访问使用的设备
        if (strpos($user_agent, 'MicroMessenger') == true) {
            $ob->class="3";
        } else if ($is_m->isMobile()) {
            $ob->class="2";
        } else {
            $ob->class="1";
        }
        $ob->save();
        return $next($request);
    }
}